<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-10-22 00:42:10
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-11-12 00:17:52
-->
<template>
  <a-modal
    :open="visible"
    :keyboard="false"
    :maskClosable="false"
    title="设置权限"
    @cancel="onCancel"
    @ok="onSubmit"
    :confirmLoading="submitLoading"
    :width="800"
  >
    <div class="edit-permissions">
      <a-tree
        :tree-data="allPermissionItems"
        :multiple="true"
        :checkable="true"
        :blockNode="true"
        :defaultExpandAll="true"
        v-model:checkedKeys="grantedPermissions"
        :field-names="{
          children: 'children',
          title: 'displayName',
          key: 'name',
        }"
      ></a-tree>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
import { useAppStore } from "@/store";
import { onMounted, ref } from "vue";
import api from "@/api/system/permissionManagement";
import { PermissionDefinitionDto } from "@/api/system/types";
const props = defineProps({
  visible: { type: Boolean, required: true },
  providerName: { type: String, required: true },
  providerKey: { type: String, required: true },
});
const appStore = useAppStore();

const emits = defineEmits(["update:visible", "success"]);
const loading = ref(true);
const submitLoading = ref(false);
const igorePermissionNames = ["AbpIdentity.UserLookup"];

/**
 * 权限定义数据
 */
const allPermissionItems = ref<Array<any>>([]);

/**
 * 拥有的权限数据
 */
const grantedPermissions = ref<Array<string>>([]);

/**
 *
 * @param items
 */
const createPutPermissions = (items: Array<PermissionDefinitionDto>) => {
  let arrs: Array<any> = [];
  items
    .filter((n) => igorePermissionNames.findIndex((x) => x == n.name) == -1)
    .map((x) => {
      arrs.push({
        name: x.name,
        isGranted: grantedPermissions.value.indexOf(x.name) !== -1,
      });
      if (x.children?.length) {
        arrs.push(...createPutPermissions(x.children));
      }
    });
  return arrs;
};

/**
 * 取消
 */
const onCancel = () => {
  emits("update:visible", false);
};

/**
 * 保存权限数据
 */
const onSubmit = async () => {
  submitLoading.value = true;
  await api
    .savePermissions({
      providerKey: props.providerKey,
      providerName: props.providerName,
      permissions: createPutPermissions(allPermissionItems.value),
    })
    .finally(() => {
      submitLoading.value = false;
    });
  emits("success");
  emits("update:visible", false);
};

/**
 * 加载权限数据
 */
const loadPermissions = async () => {
  allPermissionItems.value = await appStore.getPermissionDeninitions();

  //加载权限数据
  let res = await api.getPermissions({
    providerKey: props.providerKey,
    providerName: props.providerName,
  });

  let grantedItems: Array<string> = [];
  res.groups.map((group) => {
    grantedItems.push(
      ...group.permissions.filter((x) => x.isGranted).map((x) => x.name)
    );
  });
  grantedPermissions.value = grantedItems;
};

onMounted(() => {
  loadPermissions();
});
</script>

<style lang="less" scoped>
.edit-permissions {
  height: 600px;
  overflow: auto;
}
</style>
